using System;
using System.Data;
using System.Windows.Forms;
using Company.BLL;
using Company.BLL.GC;
using Company.BLL.DuLieuChuan;
using Janus.Windows.GridEX;
using Company.Interface.GC.PCTFormTemplate;
using Company.BLL.KDT;

namespace Company.Interface.GC
{
    public partial class PhuKienGCRegistedForm : BaseForm
    {
        public PhuKienGCRegistedForm()
        {
            InitializeComponent();
        }

        public void BindData()
        {
            string where = string.Format("MaDoanhNghiep = '{0}'", GlobalSettings.MA_DON_VI);

        }

        //-----------------------------------------------------------------------------------------
        private void BindHopDong()
        {
            string maHQ = ctrDonViHaiQuan.Ma;
            Company.BLL.GC.HopDong hopdong = new Company.BLL.GC.HopDong();
            HopDongCollection collectionHD;
            if (maHQ.Length > 0)
            {
                string where = string.Format("[MaHaiQuan] LIKE '%{0}%'", maHQ);
                collectionHD = hopdong.SelectCollectionDynamic(where, "");
            }
            else
            {
                collectionHD = hopdong.SelectCollectionAll();
            }
            hopdong.SoHopDong="";
            collectionHD.Add(hopdong);
            cbHopDong.DataSource = collectionHD;
            cbHopDong.DisplayMember = "SoHopDong";
            cbHopDong.ValueMember = "SoHopDong";
            try
            {
                cbHopDong.SelectedIndex = collectionHD.Count - 1; ;
            }
            catch { }
        }
        private void khoitao_DuLieuChuan()
        {
            // Đơn vị tính.
            this._DonViTinh = DonViTinh.SelectAll();

            // Đơn vị Hải quan.
              }

        //-----------------------------------------------------------------------------------------

        private void PhuKienGCRegistedForm_Load(object sender, EventArgs e)
        {
            this.khoitao_DuLieuChuan();
            BindHopDong();
        }

        //-----------------------------------------------------------------------------------------
        
     
      

        private void ctrDonViHaiQuan_ValueChanged(object sender, EventArgs e)
        {
            BindHopDong();
        }

       

       

        private void dgList_LoadingRow_1(object sender, RowLoadEventArgs e)
        {
            e.Row.Cells[4].Text = this.LoaiPhuKien_GetName(e.Row.Cells[3].Text);
        }

        private void dgList_RowDoubleClick_1(object sender, RowActionEventArgs e)
        {
            PhuKienDangKy pk = new PhuKienDangKy();
            pk.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
            pk.MahaiQuan = ctrDonViHaiQuan.Ma;
            pk.SoHopDong = e.Row.Cells[5].Text;
            pk.NgayKy = Convert.ToDateTime(cbHopDong.DropDownList.GetRow().Cells["NgayKy"].Text);                    
            pk.SoPhuKien = e.Row.Cells[1].Text;
            pk.NgayPhuKien = Convert.ToDateTime(e.Row.Cells[2].Text);
            pk.MaPhuKien = e.Row.Cells[3].Text;            
            switch (pk.MaPhuKien.Trim())
            {
                case "H06": { showPKHD(pk, e.Row.Cells[4].Text); } break;
                case "H10": { showPKHD(pk, e.Row.Cells[4].Text); } break;
                case "H11": { showPKHD(pk, e.Row.Cells[4].Text); } break;
                case "N01": { showPKNPL(pk, e.Row.Cells[4].Text); } break;
                case "N05": { showPKNPL(pk, e.Row.Cells[4].Text); } break;
                case "N06": { showPKNPL(pk, e.Row.Cells[4].Text); } break;
                case "N11": { showPKNPL(pk, e.Row.Cells[4].Text); } break;
                case "S06": { showPKSP(pk, e.Row.Cells[4].Text); } break;
                case "S10": { showPKSP(pk, e.Row.Cells[4].Text); } break;
                case "S13": { showPKSP(pk, e.Row.Cells[4].Text); } break;
                case "S15": { showPKNhomSP(pk, e.Row.Cells[4].Text); } break;
                case "T01": { showPKThietBi(pk, e.Row.Cells[4].Text); } break;
                case "T07": { showPKThietBi(pk, e.Row.Cells[4].Text); } break;
            }
        }
        private void showPKNPL(PhuKienDangKy pk,string name)
        {
            BoSungNPLFormRegsiter f = new BoSungNPLFormRegsiter();
            f.pkdk = pk;
            f.FormName = name;
            f.ShowDialog();
        }
        private void showPKSP(PhuKienDangKy pk, string name)
        {
            PKChiTietMaSPFormRegister f = new PKChiTietMaSPFormRegister();
            f.pkdk = pk;
            f.FormName = name;
            f.ShowDialog();
        }
        private void showPKNhomSP(PhuKienDangKy pk, string name)
        {
            PKBoSungNhomSPFormResgister f = new PKBoSungNhomSPFormResgister();
            f.pkdk = pk;
            f.FormName = name;
            f.ShowDialog();
        }
        private void showPKThietBi(PhuKienDangKy pk, string name)
        {
            PKBoSungTBFormRegister f = new PKBoSungTBFormRegister();
            f.pkdk = pk;
            f.FormName = name;
            f.ShowDialog();
        }
        private void showPKHD(PhuKienDangKy pk, string name)
        {
            PKGiahanHDFormRegister f=new PKGiahanHDFormRegister();
            f.pk=pk;
            f.FormName = name;
            f.ShowDialog();
        }

        private void dgList_DeletingRecord(object sender, RowActionCancelEventArgs e)
        {
            
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string where = "1=1";
            if (txtSoPhuKien.Text.Trim().Length > 0)
            {
                where += " and sophukien='" + txtSoPhuKien.Text+"'";
            }
            if (cbHopDong.Text.Length > 0)
            {
                where += " and sohopdong='" + cbHopDong.Text.ToString()+"'";
            }
            if (ccNgayPhuKien.Text.Length > 0)
            {
                where += " and NgayPhuKien='" + ccNgayPhuKien.Value.Month.ToString() + "/" + ccNgayPhuKien.Value.Day.ToString() + "/" + ccNgayPhuKien.Value.Year + "'";
            }
            dgList.DataSource = (new PhuKienDangKy()).SelectCollectionDynamicRegister(where, "");
        }

        private void cmMainHDGC_CommandClick(object sender, Janus.Windows.UI.CommandBars.CommandEventArgs e)
        {
            switch(e.Command.Key)
            {
                case "Dong": this.Close(); break;
                case "NhanDuLieu": DongBo(); break;
            }
        }
        private void DongBo()
        {
            WSForm wsForm = new WSForm();
            wsForm.ShowDialog(this);
            if (!wsForm.IsReady) return;
            try
            {
                this.Cursor = Cursors.WaitCursor;
                if (cbHopDong.Text.Trim().Length > 0)
                {
                    DateTime ngayky = Convert.ToDateTime(cbHopDong.DropDownList.GetRow().Cells["NgayKy"].Text);
                    PhuKienDangKy.DongBoHaiQuan(ctrDonViHaiQuan.Ma, GlobalSettings.MA_DON_VI, cbHopDong.Text, ngayky);
                }
                else
                    PhuKienDangKy.DongBoHaiQuan(ctrDonViHaiQuan.Ma, GlobalSettings.MA_DON_VI);
                btnSearch_Click(null, null);
                this.Cursor = Cursors.Default;
                ShowMessage("Đã cập nhật thành công", false);
            }
            catch (Exception ex)
            {
                this.Cursor = Cursors.Default;
                if (ShowMessage("Có lỗi khi thực hiện. Bạn có muốn đưa vào hàng đợi không" + ex.Message, true) == "Yes")
                {
                    HangDoi hd = new HangDoi();
                    hd.ID = 0;
                    hd.MaHaiQuan = ctrDonViHaiQuan.Ma;
                    hd.SoHopDong = cbHopDong.Text;
                    if (cbHopDong.Text.Trim().Length > 0)
                    {
                        DateTime ngayky = Convert.ToDateTime(cbHopDong.DropDownList.GetRow().Cells["NgayKy"].Text);
                        hd.NgayKy = ngayky;
                    }   
                    hd.LoaiToKhai = LoaiToKhai.PHU_KIEN;
                    hd.TrangThai = 1;
                    hd.ChucNang = ChucNang.NHAN_THONG_TIN;
                    MainForm.AddToQueueForm(hd);
                    MainForm.ShowQueueForm();
                }
            }       
        }
        //-----------------------------------------------------------------------------------------
    }
}